package com.lyk.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.entity.A_transfer;
import com.common.entity.PageBean;
import com.common.utils.StringUtils;
import com.cxh.dao.A_goodsDao;
import com.cxh.dao.impl.A_goodsDaoImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lyk.service.A_transferService;
import com.lyk.service.impl.A_transferServiceImpl;

public class A_transferServlet extends HttpServlet {

	@Override
	public void init() throws ServletException {	
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		String opr=request.getParameter("opr");
		if("del".equals(opr)){
			A_transferService transferService = new A_transferServiceImpl();
			int num = 0;
			String errMsg = "";
			try{
				Integer n = StringUtils.str2Integer(request.getParameter("transfer_id"),0);
				if(n != null){
					num = transferService.delA_customer(n);
				}else{
					throw new RuntimeException("交易记录编号必须输入");
				}
			}catch(Exception e){
				e.printStackTrace();
				errMsg = e.getMessage();
			}
			PrintWriter out=resp.getWriter();
			if(num>0){
				out.println("<script type='text/javascript'>alert('删除成功');location.href='transfer.jsp';</script>");
			}else{
				out.println("<script type='text/javascript'>alert('删除失败"+errMsg+"');history.back();</script>");
			}
		}else{
			list(request,resp);
		}
	}
	private void list(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		A_goodsDao goodsDao = new A_goodsDaoImpl();
		Integer n = goodsDao.countGoodsType(1);
		Integer s = goodsDao.countGoods();
		int a = (int)(n*1.0/s*1.0*100);
		request.setAttribute("a", a);
		
		A_goodsDao goodsDao3 = new A_goodsDaoImpl();
		Integer n3 = goodsDao3.countGoodsType(2);
		Integer s3 = goodsDao3.countGoods();
		int a3 = (int)(n3*1.0/s3*1.0*100);
		request.setAttribute("a3", a3);
		
		A_goodsDao goodsDao4 = new A_goodsDaoImpl();
		Integer n4 = goodsDao4.countGoodsType(3);
		Integer s4 = goodsDao4.countGoods();
		int a4 = (int)(n4*1.0/s4*1.0*100);
		request.setAttribute("a4", a4);
		
		A_goodsDao goodsDao5 = new A_goodsDaoImpl();
		Integer n5 = goodsDao5.countGoodsType(4);
		Integer s5 = goodsDao5.countGoods();
		int a5 = (int)(n5*1.0/s5*1.0*100);
		request.setAttribute("a5", a5);
		A_goodsDao goodsDao6 = new A_goodsDaoImpl();
		Integer n6 = goodsDao6.countGoodsType(5);
		Integer s6 = goodsDao6.countGoods();
		int a6 = (int)(n6*1.0/s6*1.0*100);
		request.setAttribute("a6", a6);
		A_goodsDao goodsDao7 = new A_goodsDaoImpl();
		Integer n7 = goodsDao7.countGoodsType(6);
		Integer s7 = goodsDao7.countGoods();
		int a7 = (int)(n7*1.0/s7*1.0*100);
		request.setAttribute("a7", a7);
		Map<String, Object> map=new HashMap<String, Object>();
		Integer transfer_id = StringUtils.str2Integer(request.getParameter("transfer_id"), 0);
		if(transfer_id != 0){
			map.put("transfer_id",transfer_id);
		}
		String date = request.getParameter("transfer_date");
		if(StringUtils.isNotNull(date)){
			map.put("transfer_date",date);
		}
		A_transferService transferService=new A_transferServiceImpl();
		Integer pageIndex=StringUtils.str2Integer(request.getParameter("pageIndex"), 1);
		Integer pageSize = StringUtils.str2Integer(request.getParameter("pageSize"), 3);
		PageBean<A_transfer> pageBean=transferService.findPageByCondition(pageIndex, pageSize, map);
		request.setAttribute("pageBean", pageBean);
		request.getRequestDispatcher("transfer.jsp").forward(request, resp);
		/*ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(pageBean);
		PrintWriter out=resp.getWriter();
		out.print(json);*/
	}
}
